package android.meetingatsb.location


import android.meetingatsb.MainActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.LocationSource
import com.amap.api.maps2d.MapFragment
import com.amap.api.maps2d.model.Marker
import com.amap.api.maps2d.model.MyLocationStyle
import com.amap.api.maps2d.overlay.PoiOverlay
import com.amap.api.services.core.AMapException
import com.amap.api.services.core.PoiItem
import com.amap.api.services.poisearch.PoiResult
import com.amap.api.services.poisearch.PoiSearch

class NewMapFragment : MapFragment(), LocationSource, AMapLocationListener, PoiSearch.OnPoiSearchListener, AMap.OnMarkerClickListener {


    lateinit var mListener: LocationSource.OnLocationChangedListener
    lateinit var mlocationClient: AMapLocationClient
    lateinit var mLocationOption: AMapLocationClientOption

    companion object {
        private var mapFragment: NewMapFragment? = null
        fun getIntance() {
            if (mapFragment == null) NewMapFragment()
            mapFragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        setStyle()
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    private fun setStyle() {
        map.setLocationSource(this)
        // 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        map.isMyLocationEnabled = true
        //蓝点初始化
        val myLocationStyle = MyLocationStyle()
        myLocationStyle.interval(2000)
        map.setMyLocationStyle(myLocationStyle)
        map.uiSettings.isMyLocationButtonEnabled = true
        map.isMyLocationEnabled = true
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER)
        myLocationStyle.showMyLocation(true)
        map.setOnMarkerClickListener(this)
    }

    //激活定位
    override fun activate(onLocationChangedListener: LocationSource.OnLocationChangedListener) {
        mListener = onLocationChangedListener
        //初始化定位
        mlocationClient = AMapLocationClient(activity)
        //初始化定位参数
        mLocationOption = AMapLocationClientOption()
        //设置定位回调监听
        mlocationClient.setLocationListener(this)
        //设置为高精度定位模式
        mLocationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
        //设置合适的定位时间的间隔（最小间隔支持为2000ms）
        mLocationOption.interval = 50000
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption)
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        mlocationClient.startLocation()
    }

    override fun onLocationChanged(aMapLocation: AMapLocation?) {
        if (aMapLocation != null && aMapLocation.errorCode == 0) {
            mListener.onLocationChanged(aMapLocation)
        }
    }

    //停止定位

    override fun deactivate() {
        mlocationClient.stopLocation()
        mlocationClient.onDestroy()
    }

    override fun onDestroy() {
        super.onDestroy()
        mlocationClient.onDestroy()
    }

    //-----------获取poi-----------
    fun getDatingLocation(keyword: String) {
        val query = PoiSearch.Query(keyword.toString(), "", "广州市")// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
        query.pageSize = 10
        query.pageNum = 0
        query.cityLimit = true

        val poiSearch = PoiSearch(activity, query)
        poiSearch.setOnPoiSearchListener(this)
        poiSearch.searchPOIAsyn()
    }

    override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
    }

    override fun onPoiSearched(result: PoiResult?, rCode: Int) {
        if (rCode == AMapException.CODE_AMAP_SUCCESS) {
            val poiItems = result?.pois
            map.clear()
            val poiOverlay = PoiOverlay(map, poiItems)
            poiOverlay.removeFromMap()
            poiOverlay.addToMap()
            poiOverlay.zoomToSpan()
        }

    }

    //点击 marker 时返回的信息
    override fun onMarkerClick(marker: Marker?): Boolean {
        marker?.showInfoWindow()
        if (activity is MainActivity) {
            (activity as MainActivity).setLocation(marker?.title + " " + marker?.snippet)
        }
        return true
    }
}